package mirrormonkey.state.member.outbound;

public class TrackingOutboundDynamicMemberStateData extends
		OutboundDynamicMemberStateData {

	public boolean lastWasNull;

	public int lastHashCode;

	public TrackingOutboundDynamicMemberStateData() {
		lastWasNull = true;
		lastHashCode = 0;
	}

	public static final int hashCode(final Object value) {
		return value == null ? 0 : value.hashCode();
	}

	public void setValue(Object newValue) {
		lastWasNull = newValue == null;
		lastHashCode = hashCode(hashCode(newValue));
	}

	public final boolean valueChanged(Object newValue) {
		if (lastWasNull || newValue == null) {
			return lastWasNull != (newValue == null);
		}
		return lastHashCode != newValue.hashCode();
	}

	@Override
	public boolean valueUpdated(Object newValue) {
		if (valueChanged(newValue)) {
			setValue(newValue);
			return true;
		}
		return false;
	}

}
